﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Http;
using Web_API_Test.Models;

namespace Web_API_Test.Controllers
{
    [RoutePrefix("api/Test")]
    public class TestController: ApiController
    {
        [Route("Hello_Get")]
        [HttpGet]
        public string GetHelloWorldByGet(int num1,string str1)
        {
            string str = $"Hello World By Get:{num1}:{str1}" ;
            var res = (str);
            return res;
        }

        [Route("Hello_Post")]
        [HttpPost]
        public string GetHelloWorldByPost([FromBody]TestBody param)
        {
            return $"Hello World By Post:{param.shuzi}:{param.str}";
        }

        [Route("ExportExcel")]
        [HttpGet]
        public HttpResponseMessage Export(string filePath)
        {
            //输出到浏览器
            try
            {
                string strPath = filePath;
                var list = strPath.Split('\\').ToList();
                string fileName = list[list.Count - 1];
                var stream = new FileStream(strPath, FileMode.Open);
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new StreamContent(stream);
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = fileName
                };

                return response;
            }
            catch
            {
                return new HttpResponseMessage(HttpStatusCode.NoContent);
            }
        }
    }
}